package com.zd.bms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zd.bms.bean.vo.resp.IndexStatisRespVo;
import com.zd.bms.service.IBmsUserService;
import com.zd.bms.service.IIndexService;
import com.zd.bms.service.IScenicSpotService;
import com.zd.bms.service.ISysLogService;
import com.zd.common.bean.model.BmsUser;
import com.zd.common.bean.model.SysLog;
import com.zd.common.common.enums.BmsUserTypeEnum;
import com.zd.common.common.enums.StatusEnum;
import com.zd.common.common.utils.StringUtils;

@Controller
@RequestMapping("index")
public class IndexController extends BaseController {
	@Autowired
	private IBmsUserService bmsUserService;

	@Autowired
	private IIndexService indexService;

	@Autowired
	private ISysLogService sysLogService;

	@Autowired
	private IScenicSpotService scenicSpotService;

	@RequestMapping
	public ModelAndView index(HttpServletRequest req, String errorMsg) throws Exception {
		ModelAndView mv = new ModelAndView();
		BmsUser bmsUser = getSessionUser();
		if (bmsUser == null) {
			mv.setViewName("index/login");
			mv.addObject("errorMsg", errorMsg);
		} else {
			if (bmsUser.getType() == BmsUserTypeEnum.Scenic_spot_conductor.getKey()) {
				mv.setViewName("/sellTicket/index");
				mv.addObject("user", getSession("user"));
				return mv;
			}
			mv.setViewName("/index/index");
			mv.addObject("user", getSession("user"));
			mv.addObject("roleName", getSession("roleName"));
			mv.addObject("menuTree", getSession("menuTree"));
			mv.addObject("menuTreeJson", JSONObject.toJSONString(getSession("menuTree")));
		}
		return mv;
	}

	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest req, String username, String password) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/index");

		SysLog sysLog = new SysLog();
		sysLog.setActionUri("/index/login");
		sysLog.setScenicSpotId(0);
		sysLog.setUserId(0);
		sysLog.setUserEntNo(username);
		sysLog.setActionName("登录");
		sysLog.setParams(null);
		sysLog.setCreateTime(new Date());
		sysLog.setUpdateTime(new Date());
		sysLogService.insert(sysLog);

		if (StringUtils.isBank(username) || StringUtils.isBank(password)) {
			mv.addObject("errorMsg", "请输入用户名和密码!");
		} else {
			BmsUser bmsUser = bmsUserService.findUser(username, password);
			if (bmsUser == null) {
				mv.addObject("errorMsg", "用户名/密码错误!");
			} else if (bmsUser.getStatus() == StatusEnum.invalid.getKey()) {
				mv.addObject("errorMsg", "账号已经被禁用，请联系管理员!");
			} else {
				Map<String, Object> resultMap = bmsUserService.loadBmsUserRoleResource(bmsUser);
				setCookieUser(resultMap);
			}
		}
		return mv;
	}

	@RequestMapping("/toModify")
	public ModelAndView toModify() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index/modify");
		return mv;
	}

	@RequestMapping("modify")
	@ResponseBody
	public Map<String, Object> modeify(String oldPwd, String newPwd) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.putAll(bmsUserService.updatePwd(oldPwd, newPwd, getSessionUser()));
		return resultMap;
	}

	@RequestMapping("/exit")
	public ModelAndView exit() throws Exception {
		ModelAndView mv = new ModelAndView();

		BmsUser bmsUser = getSessionUser();
		if (bmsUser != null) {
			Integer scenicSpotId = bmsUser.getScenicSpotId();
			SysLog sysLog = new SysLog();
			sysLog.setActionUri("/index/exit");
			sysLog.setScenicSpotId(scenicSpotId);
			sysLog.setUserId(bmsUser.getId());
			sysLog.setUserEntNo(bmsUser.getEntNo());
			sysLog.setActionName("退出");
			sysLog.setParams(null);
			sysLog.setCreateTime(new Date());
			sysLog.setUpdateTime(new Date());
			sysLogService.insert(sysLog);
		}

		removeAllSession();
		mv.setViewName("index/login");
		return mv;
	}

	@RequestMapping("/welcome")
	public ModelAndView welcome() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index/welcome");

		BmsUser bmsUser = getSessionUser();
		if (bmsUser != null) {
			Integer scenicSpotId = bmsUser.getScenicSpotId();
			IndexStatisRespVo respVo = null;
			if (scenicSpotId != null && scenicSpotId != 0) {
				respVo = indexService.indexStatis(scenicSpotId);
			} else {
				List<Map<String, Object>> scenicSpotList = scenicSpotService.getScenicSpotKeyValueList();
				respVo = indexService.indexStatis(null);
				mv.addObject("scenicSpotList", scenicSpotList);
			}
			mv.addObject("entryNums", respVo.getEntryNums());
			mv.addObject("onlineSellNums", respVo.getOnlineSellNums());
			mv.addObject("outlineSellNums", respVo.getOutlineSellNums());
			mv.addObject("sellTickStatisLast7DayList",
					JSONObject.toJSON(respVo.getSellTickStatisLast7DayList()).toString());
			mv.addObject("pieData", JSONObject.toJSON(respVo.getSellTickStatisPieData()).toString());
			mv.addObject("channelPieData", JSONObject.toJSON(respVo.getChannelPieData()).toString());
		}
		return mv;
	}

	@RequestMapping("/scenicSpotStatistics")
	@ResponseBody
	public Map<String, Object> scenicSpotStatistics(Integer scenicSpotId) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		IndexStatisRespVo respVo = null;
		respVo = indexService.indexStatis(null);
		resultMap.put("ruslt", true);
		resultMap.put("entryNums", respVo.getEntryNums());
		resultMap.put("onlineSellNums", respVo.getOnlineSellNums());
		resultMap.put("outlineSellNums", respVo.getOutlineSellNums());
		resultMap.put("sellTickStatisLast7DayList",
				JSONObject.toJSON(respVo.getSellTickStatisLast7DayList()).toString());
		resultMap.put("pieData", JSONObject.toJSON(respVo.getSellTickStatisPieData()).toString());
		resultMap.put("channelPieData", JSONObject.toJSON(respVo.getChannelPieData()).toString());
		return resultMap;
	}
}
